var request = require('../../utils/request');

Page({
  data: {
    second: 60,
    selected: true,
    selected1: false,
    hasValue: false,
    disable: true,
    disable2: true,
    value :'',
    testNum:'',
    currentPage:'',
    testText:'获取验证码'
  },

  onLoad: function (options){
    this.setData({
      value:options.tel,
      currentPage:options.current
    });
    if (this.data.value == '') {
      this.setData({
        hasValue: false,
        disable: true,
      });
    }else{
      this.setData({
        hasValue: true,
        disable: false,
      });
    }
  },

  onShow: function(){

  },

  getNum: function (e) {
    var that = this;
    var re = /^1\d{10}$/;
    if (re.test(this.data.value)) {
      //console.log("正确");
    } else {
      wx.showModal({
        content: '请输入有效的手机号',
        showCancel: false,
      })
      return;
    }
    //获取验证码
    var id = wx.getStorageSync('sessionId');
    var num = this.data.value;
    var data = {
      'self:sms:generate':{
        'sms_type':'',
        'mobile':num,
        'wx_session':id
      }
    }
    request.post({
      path: '/wxapp_api/api'
    },data)
      .then(function (res) {
        var retData = res['self:sms:generate'];
        if (retData.ret != 0) {
          wx.showModal({
            content: retData.msg,
            showCancel:false
          })
          return;
        }
        that.setData({
          selected: false,
          selected1: true,
          disable2: false,
          testText:'重新获取'
        });
        countdown(that);
      });
      

  },

  changeColor:function(e){
    var phone = e.detail.value;
    this.setData({
      value: phone,
    });
    if (phone == "") {
      this.setData({
        hasValue: false,
        disable: true,
      });
    }else{
      this.setData({
        hasValue: true,
        disable: false,
      });
    }
  },

  changeTest: function(e){
    var test = e.detail.value ;
    this.setData({
      testNum:test
    })
  },

  goback: function(){
    var cur = this.data.currentPage;
    if (cur == "pages/center/center") {
      wx.switchTab({ 
        url: '../new-index/index', 
        success: function (e) { 
          var page = getCurrentPages().pop(); 
          if (page == undefined || page == null) return; 
          page.onLoad(); 
        } 
      })
    }else{
      wx.navigateBack()
    }
    
  },
  gohome: function(){
    wx.switchTab({ 
      url: '../new-index/index', 
      success: function (e) { 
        var page = getCurrentPages().pop(); 
        if (page == undefined || page == null) return; 
        page.onLoad(); 
      } 
    })
  },

  cancel: function() {
    //var cur = this.data.currentPage;
    request.nobind();
    wx.navigateBack()
  },

  submit:function(){
    var temp = this.data.testNum;
    if (temp == ""){
      wx.showModal({
        content: "请先输入验证码",
        showCancel: false
      });
      return;
    } else if (temp.length < 6){
      wx.showModal({
        content: "验证码不正确",
        showCancel: false
      });
      return;
    }
    var id = wx.getStorageSync('sessionId');
    var num = this.data.value;
    var data = {
      'self:sms:validate':{
        'sms_type':'',
        'mobile':num,
        'code':temp,
        'wx_session':id
      }
    }
    request.post({
      path: '/wxapp_api/api'
    },data)
      .then(function (res) {
        var retData = res['self:sms:validate'];
        if (retData.ret != 0) {
          wx.showModal({
            content: retData.errorcode,
            showCancel:false
          })
          return;
        }else{
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 1900
          })
        }
        //成功后返回前一页
        var backTo = setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
      });
  },

});

function countdown(that) {
  var second = that.data.second;
  if (second == 0) {
    // console.log("Time Out...");
    that.setData({
      selected: true,
      selected1: false,
      second: 60,
    });
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      second: second - 1,
    });
    countdown(that);
  }
    , 1000)
}
